package com.zkool.mysite.run;

import org.apache.log4j.Logger;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.dialect.OracleDialect;
import com.jfinal.plugin.activerecord.dialect.PostgreSqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.zkool.mysite.common.DictKeys;

public class JfinalConfig extends JFinalConfig {

	private static Logger log = Logger.getLogger(JfinalConfig.class);
	
	@Override
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用PropKit.get(...)获取值
		PropKit.use("mysite.properties");
		me.setDevMode(PropKit.getBoolean(DictKeys.config_devMode, false));
	}

	@Override
	public void configRoute(Routes me) {
		
	}
	

	@Override
	public void configPlugin(Plugins me) {
		//初始化多数据源
    	String dataSourceStr = PropKit.get(DictKeys.db_dataSource);
    	if(StrKit.notBlank(dataSourceStr)){
    		String[] dataSources = dataSourceStr.split(",");
        	log.info("开始初始化数据源,总共有"+dataSources.length+"个数据源");
    		for(String dataSource:dataSources){
    			log.info("configPlugin 配置Druid数据库连接池连接属性");
    			DruidPlugin druidPlugin = new DruidPlugin(PropKit.get(dataSource+"."+DictKeys.db_connection_jdbcUrl), 
    					PropKit.get(dataSource+"."+DictKeys.db_connection_userName), 
    					PropKit.get(dataSource+"."+DictKeys.db_connection_passWord).trim(),
    					PropKit.get(dataSource+"."+DictKeys.db_connection_driverClass));
    			
    			log.info("configPlugin 配置Druid数据库连接池大小");
    			druidPlugin.set(
    					PropKit.getInt(DictKeys.db_initialSize), 
    					PropKit.getInt(DictKeys.db_minIdle), 
    					PropKit.getInt(DictKeys.db_maxActive));
    			ActiveRecordPlugin arp = new ActiveRecordPlugin(dataSource, druidPlugin);
    			boolean devMode = PropKit.getBoolean(DictKeys.config_devMode, false);
    			arp.setDevMode(devMode); // 设置开发模式
    			arp.setShowSql(devMode); // 是否显示SQL
    			arp.setContainerFactory(new CaseInsensitiveContainerFactory(true));// 大小写不敏感
    			
    			log.info("configPlugin 数据库类型判断");
    			String db_type = PropKit.get(dataSource+"."+DictKeys.db_type_key);
    			if(db_type.equals(DictKeys.db_type_postgresql)){
    				log.info("configPlugin 使用数据库类型是 postgresql");
    				arp.setDialect(new PostgreSqlDialect());
    				
    			}else if(db_type.equals(DictKeys.db_type_mysql)){
    				log.info("configPlugin 使用数据库类型是 mysql");
    				arp.setDialect(new MysqlDialect());
    				
    			}else if(db_type.equals(DictKeys.db_type_oracle)){
    				log.info("configPlugin 使用数据库类型是 oracle");
    				druidPlugin.setValidationQuery("select 1 FROM DUAL"); //指定连接验证语句
    				arp.setDialect(new OracleDialect());
    			}
    			me.add(druidPlugin);
    			me.add(arp);
    		}
    	}
    	
	}
	
	@Override
	public void configHandler(Handlers me) {
		
	}

	@Override
	public void configInterceptor(Interceptors arg0) {

	}
	
	/**
	 * 系统启动完成后执行
	 */
	public void afterJFinalStart() {
		log.info("afterJFinalStart mysite启动 ");
	}
	
	/**
	 * 系统关闭前调用
	 */
	public void beforeJFinalStop() {
		log.info("beforeJFinalStop mysite关闭");
	}

	/**
	 * 建议使用 JFinal 手册推荐的方式启动项目
	 * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) {
//		JFinal.start("WebRoot", 8080, "/", 5);
		JFinal.start("src/main/webapp", 80, "/", 5);
	}
	
}
